layui.define(['jquery','upload'], function (exports) {
    var $ = layui.jquery,
        upload=layui.upload;

    //图片上传
    var uploadInst =upload.render({
        elem: '#image_upload',
        url: '/upload/',
        before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#demo1').attr('src', result); //图片链接（base64）
            });
        },
        done: function(res){
            //如果上传失败
            if(res.code > 0){
                return layer.msg('上传失败');
            }
            //上传成功
        }
    });

  //创建一个编辑器
    var ue = UE.getEditor('editor');
    ue.ready(function() {
        ue.setHeight(500);
        ue.setContent($('#text').val())
    });

    $(document).on('click','#submit',function(){
        var content=ue.getContent();
        var id=$("#articleId").val();
        var source=$("#source").val();
        $.post(
            path+"/api/saveArticle",
            {articleId:id,content:content,source:source},
            function (data) {
                if(data.success){
                    layer.alert("文章保存成功")
                }else {
                    layer.alert("文章保存失败")
                }
            }
            ,'json'
        )
    });

    $(document).on('click','#clear',function(){
        ue.setContent('');
        $("#source").val('');
    });

    /**
     * 导入对应的JS模块
     */
    exports('articleDetail', {}); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});